﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using IsikFx.Project.ClientApp.Helpers;
using log4net;
namespace IsikFx.Project.ClientApp
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        private const int MINIMUM_SPLASH_TIME = 2500; // Miliseconds
        private const int SPLASH_FADE_TIME = 1000;     // Miliseconds
        public static ILog logger = LogManager.GetLogger(typeof(App));

        protected override void OnStartup(StartupEventArgs e)
        {
            log4net.Config.XmlConfigurator.Configure();
            logger.Debug("Program başladı.");
            SplashScreen splash = new SplashScreen("Content/SplashScreen.jpg");
            splash.Show(false);

            Stopwatch timer = new Stopwatch();
            timer.Start();

            //Make sure that the splash screen lasts at least two seconds
            timer.Stop();
            int remainingTimeToShowSplash = MINIMUM_SPLASH_TIME - (int)timer.ElapsedMilliseconds;
            if (remainingTimeToShowSplash > SPLASH_FADE_TIME)
                Thread.Sleep(remainingTimeToShowSplash);

            //Show the page
            splash.Close(TimeSpan.FromMilliseconds(SPLASH_FADE_TIME));

            try
            {
                IsikFxWcfService.ServiceClient client = new IsikFxWcfService.ServiceClient();
                client.WcfIsAlive();
            }
            catch (Exception ex)
            {
                logger.Error(ex.Message);
                logger.Error(ex.StackTrace);

                MessageBox.Show("Ana bilgisayar ile bağlantı kurulamıyor lütfen daha sonra tekrar deneyiniz.");

                Thread.Sleep(5000);

                Process.GetCurrentProcess().Kill();
            }

            base.OnStartup(e);

            MainWindow main = new MainWindow();
            main.Show();
        }
    }
}
